package com.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hotel.entity.ServiceEntity;
import com.hotel.dto.ServiceItemDTO;
import com.hotel.dto.ServicePageDto;
import com.hotel.mapper.ServiceMapper;
import com.hotel.result.PageResult;
import com.hotel.service.ServiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zxq
 */
@Service
public class ServiceServiceImpl extends ServiceImpl<ServiceMapper, ServiceEntity> implements ServiceService {

    @Autowired
    private ServiceMapper serviceMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    private static final String SERVICE_KEY = "service:list";
    private static  final String SERVICE_ = "service_";
    @Override
    public PageResult list(ServicePageDto servicePageDto) {

        PageHelper.startPage(servicePageDto.getPage(),servicePageDto.getPageSize());

        Page<ServiceEntity> page = serviceMapper.list(servicePageDto);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(ServiceItemDTO service) {
        ServiceEntity serviceEntity = new ServiceEntity();
        BeanUtils.copyProperties(service, serviceEntity);
        save(serviceEntity);
    }

    @Override
    public Integer removedByIds(Integer[] ids ){
        LambdaQueryWrapper<ServiceEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ServiceEntity::getId,ids);
        List<ServiceEntity> list = list(wrapper);
        for (ServiceEntity serviceEntity : list) {
            redisTemplate.opsForHash().delete(SERVICE_KEY,SERVICE_+serviceEntity.getId());
        }
        int i = serviceMapper.deleteBatchIds(Arrays.asList(ids));
        return i ;
    }

    @Override
    public List<ServiceEntity> getall() {
        List<Object> values =  redisTemplate.opsForHash().values(SERVICE_KEY);
        if (ObjectUtils.isEmpty(values)) {
            LambdaQueryWrapper<ServiceEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ServiceEntity::getStatus,1);
            List<ServiceEntity> list = list(wrapper);
            for (ServiceEntity serviceEntity : list) {
                redisTemplate.opsForHash().put(SERVICE_KEY,SERVICE_+serviceEntity.getId(),serviceEntity);
            }
            return list;
        }
        List<ServiceEntity> list= new ArrayList<>();
        for (Object value : values) {
            if (value instanceof ServiceEntity)
                list.add((ServiceEntity) value);
        }
        return list;
    }
}
